package com.wh.service.instorehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.mapper.initial.ProductMapper;
import com.wh.mapper.initial.StorehouseMapper;
import com.wh.mapper.initial.SupplierMapper;
import com.wh.mapper.instorehouse.PurchaseMapper;
import com.wh.pojo.*;
import com.wh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService{

    @Autowired
    private StorehouseMapper storehouseMapper;

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Override
    public PageResult getPurchaseList(PageResult pageResult) {
        Page<Purchase> purchasePage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Purchase> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"purchase_id",pageResult.getQuery());

        purchasePage = purchaseMapper.selectPage(purchasePage,queryWrapper);
        pageResult.setTotal((long) purchasePage.getRecords().size()).setRows(purchasePage.getRecords());
//        System.out.println(purchasePage.getRecords().size());
        return pageResult;
    }

    @Override
    public void addPurchase(Purchase purchase) {
        purchaseMapper.insert(purchase);
    }

    @Override
    public List<Product> getProductList(String name) {
        boolean flag = StringUtils.hasLength(name);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(flag,"name",name);
        List<Product> productList = productMapper.selectList(queryWrapper);
        return productList;

    }

    @Override
    public List<Supplier> getSupplier() {
        List<Supplier> supplierList = supplierMapper.selectList(null);
        return supplierList;
    }

    @Override
    public List<Storehouse> getStorehouse() {
//        QueryWrapper<Storehouse> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("type","入库区");
//        return storehouseMapper.selectList(queryWrapper);
        return storehouseMapper.selectList(null);
    }

    @Override
    public void deletePurchase(String purchaseId) {
        purchaseMapper.deleteById(purchaseId);
    }

    @Override
    public void updataPurchase(Purchase purchase) {
        purchaseMapper.updateById(purchase);
    }

}
